<?php
namespace App\services;


/**
 * 上传类,分块上传
 */
class Upload
{
    private $request;
    private $error = false;
    private $appName = '';
    private $formName = 'file';

    public function __construct()
    {
        $this->request = request();
    }

    public function getError()
    {
        return $this->error;
    }

    public function setAppName($appName)
    {
        $this->appName = $appName;
    }


    public function setFormName($name)
    {
        $this->formName = $name;
    }

    public function upload()
    {

        $app = $this->appName ?? 'public';

        $fileImage    = $this->request->file($this->formName);
        if(!$fileImage){
            $this->error = "文件不存在！";
            return false;
        }

        $originalName = $fileImage->getUploadName();
        $strFileExtension = strtolower($fileImage->getUploadExtension());
        $strDate    = date('Ymd');
        $fileSaveName    = (empty($app) ? '' : $app . '/'). $strDate . '/' . md5(uniqid()) . "." . $strFileExtension;
        try{
            OSS::publicUpload($fileSaveName, $fileImage->getRealPath());
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }

        $urlPrefix = config('admin.oss_url_prefix', 'https://shengxuetan-static.pxxedu.com');

        return [
            "name"        => $originalName,
            'url'         => $urlPrefix.'/'.$fileSaveName
        ];
    }


}
